package com.chancingpack.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 登录过滤
 *
 * @author geloin
 * @date 2012-4-10 下午2:37:38
 */
public class SessionFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        // 请求的uri
        String uri = request.getRequestURI();
        //不拦截登录页面
        if (uri.indexOf("/login")>-1 || uri.indexOf("/checkLogin")>-1 || uri.indexOf("/WxForselectController")>-1 ||
        uri.indexOf("/order")>-1 || uri.indexOf("/WxUserLoginController")>-1 || uri.indexOf("/myproduct")>-1 ||
        uri.indexOf("/wx")>-1  || uri.indexOf("/myBook")>-1 || uri.indexOf("/resources")>-1) {
            filterChain.doFilter(request, response);
            //拦截其他所有页面
        } else {
            String consiId=(String)request.getSession().getAttribute("custId");
            if (consiId != null) {
                //Session里面用户存在的时候不拦截
                filterChain.doFilter(request, response);
            } else {
                if( uri.indexOf("/WxForselectController")>-1 ||
                        uri.indexOf("/order")>-1 || uri.indexOf("/WxUserLoginController")>-1 || uri.indexOf("/myProduct")>-1 ||
                        uri.indexOf("/wx")>-1  || uri.indexOf("/myBook")>-1 || uri.indexOf("/resources")>-1){
                    //Session里面用户不存在的时候进行拦截
                    PrintWriter writer = response.getWriter();
//                writer.print("defeat");
                    request.setCharacterEncoding("UTF-8");
                    response.setCharacterEncoding("UTF-8");
                    //TODO 跳转页面路径
                    String loginPage = request.getSession().getServletContext().getContextPath() + "/wxLogin";
                    StringBuilder builder = new StringBuilder();
                    builder.append("<script type=\"text/javascript\">");
                    builder.append("window.top.location.href='");
                    builder.append(loginPage);
                    builder.append("';");
                    builder.append("</script>");
                    writer.print(builder.toString());
                    writer.close();
                }else
                {
                    //Session里面用户不存在的时候进行拦截
                    PrintWriter writer = response.getWriter();
//                writer.print("defeat");
                    request.setCharacterEncoding("UTF-8");
                    response.setCharacterEncoding("UTF-8");
                    //TODO 跳转页面路径
                    String loginPage = request.getSession().getServletContext().getContextPath() + "/login";
                    StringBuilder builder = new StringBuilder();
                    builder.append("<script type=\"text/javascript\">");
                    builder.append("window.top.location.href='");
                    builder.append(loginPage);
                    builder.append("';");
                    builder.append("</script>");
                    writer.print(builder.toString());
                    writer.close();
                }
                }
            }
        /*}*/
    }
}
